﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.ResumeService
{
    public static class WorkExperienceManager
    {
        public static void SaveWorkExperience(
            Guid _id,
            string _user,
            string _companyName,
            string _department,
            int _positionID,
            string _positionName,
            int _companySize,
            DateTime _beginDate,
            DateTime _overDate,
            string _description,
            int _industry,
            int _nature
            )
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_companyName)) throw new Exception("企业名称必填");
                if (string.IsNullOrEmpty(_description)) throw new Exception("工作描述必填");
                if (_companyName.Length > 30) throw new Exception("企业名称不能超过30个字符");
                if (!string.IsNullOrEmpty(_department) && _department.Length > 20) throw new Exception("所在部门不能超过20个字符");
                if (!string.IsNullOrEmpty(_description) && _description.Length > 500) throw new Exception("工作描述不能大于500个字符");
                if (string.IsNullOrEmpty(_positionName)) throw new Exception("职位名称必填");
                if (!string.IsNullOrEmpty(_positionName) && _positionName.Length > 20) throw new Exception("职位名称不能超过20个字符");
                #endregion
                
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        var _w = _resume.WorkExperience.Where(w => w.ID == _id).FirstOrDefault();
                        if (_w == null)
                        {
                            if (_resume.WorkExperience.Count() > 10) throw new Exception("工作经验记录每人最多可发10条");
                            _w = new WorkExperience();
                            _w.ID = Guid.NewGuid();
                            _w.User = _user;
                            _w.CompanyName = _companyName;
                            _w.CompanySizeID = _companySize;
                            _w.BeginDate = _beginDate;
                            _w.OverDate = _overDate;
                            _w.Department = _department;
                            _w.Description = _description;
                            _w.IndustryID = _industry;
                            _w.CompanyNatureID = _nature;
                            _w.PositionID = _positionID;
                            _w.PositionName = _positionName;

                            _adc.WorkExperience.InsertOnSubmit(_w);
                        }
                        else
                        {
                            _w.CompanyName = _companyName;
                            _w.CompanySizeID = _companySize;
                            _w.BeginDate = _beginDate;
                            _w.OverDate = _overDate;
                            _w.Department = _department;
                            _w.Description = _description;
                            _w.IndustryID = _industry;
                            _w.CompanyNatureID = _nature;
                            _w.PositionID = _positionID;
                            _w.PositionName=_positionName;
                        }
                        _resume.UpdateDate = DateTime.Now;
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存工作经验内容出错："+_e.Message);
            }
        }
        /// <summary>
        /// 获取工作经验
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static WorkExperience GetWorkExperience(Guid _id)
        {
            return DBHelper.Get<WorkExperience>(w=>w.ID==_id,"获取工作经历出错：");
        }
        /// <summary>
        /// 删除工作经验
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static bool Delete(Guid _id)
        {
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _e = _adc.WorkExperience.Where(e => e.ID == _id).FirstOrDefault();
                    _adc.WorkExperience.DeleteOnSubmit(_e);

                    _adc.SubmitChanges();

                }
            }
            catch (Exception _e)
            {
                throw new Exception("删除工作经验出错:" + _e.Message);
            }

            return true;
        }
        /// <summary>
        /// 获取工作经验列表
        /// </summary>
        /// <param name="_user"></param>
        /// <returns></returns>
        public static List<WorkExperience> GetWorkExperience(string _user)
        {
            if (string.IsNullOrEmpty(_user)) throw new Exception("用户名不能为空");

            return DBHelper.GetAll<WorkExperience>(e=>e.User==_user,"获取工作经验列表出错:");
        }
    }
}
